(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
'use strict';

(function () {
    /**
     * Date: 2017/8/18
     * Email: 506713930@qq.com
     * Author: hishion
     * 
     * [Lottery 大转盘抽奖js插件, 无依赖, 简单易用]
     * @param  {[Dom Object]} oCanvas [canvas对象]
     * @param  {[Object]}     options [配置参数, 请参考底部的_setOptions方法中的config对象]
     */
    function Lottery() {
        return this._init.apply(this, arguments);
    }
    Lottery.prototype = {
        _init: function _init(oCanvas, options) {
            if (!oCanvas) return false;

            this.oCanvas = oCanvas;
            this.options = this._setOptions(options);
            this.size = (this.options.products || []).length;
            this.angle = 2 * Math.PI / this.size;
            this.sAngle = 1.5 * Math.PI - this.angle / 2;
            this.ctx = oCanvas.getContext("2d");

            this.rotate = 0;
            /*存储图片元素*/
            this.oImages = [];
            /*存储图片链接*/
            this.imgurl = [];
            this.isOver = true;

            this._setLayout();
            this._fixOptions();

            this._draw();
            this._start();

            //this._resize();
        },
        /*修正options参数*/
        _fixOptions: function _fixOptions() {
            this.options.dpr = this.options.dpr || 1;
            this.outerRadius = (parseInt(this.options.outerRadius) || this.radius) * this.options.scale;
            this.innerRadius = (parseInt(this.options.innerRadius) || 0) * this.options.scale;
            this.options.speed = Math.min(Math.max(1, this.options.speed), 30);

            if (String(this.options.font.y).indexOf('%') > 0) {
                this.options.font.y = this.outerRadius * parseInt(this.options.font.y) / 100;
            }

            if (String(this.options.images.y).indexOf('%') > 0) {
                this.options.images.y = this.outerRadius * parseInt(this.options.images.y) / 100;
            }
        },
        /*重置canvas尺寸*/
        _setLayout: function _setLayout() {
            var oCanvas = this.oCanvas;
            var options = this.options;
            var iWidth = oCanvas.offsetWidth || oCanvas.clientWidth || oCanvas.width;

            iWidth = iWidth * this.options.scale;

            oCanvas.width = iWidth;
            oCanvas.height = iWidth;
            oCanvas.style.height = iWidth / this.options.scale + 'px';

            this.diameter = iWidth;
            this.radius = iWidth / 2;

            this.prefix('transition', this.options.transition);
        },
        /*获取文字的样式*/
        _getFontStyle: function _getFontStyle() {
            var dpr = this.options.dpr;
            var scale = this.options.scale;
            var fontStyle = this.options.font;
            return '{{style}} {{weight}} {{size}}/{{lineHeight}} {{family}}'.replace(/\{\{([^}]*)\}\}/g, function (a, b) {
                return b == 'size' ? parseInt(fontStyle[b]) * dpr * scale + 'px' : fontStyle[b];
            });
        },
        /*重置窗口*/
        /* _resize: function(){
             var self = this;
             window.addEventListener('resize', function(){
                 self._setLayout();
                 self._fixOptions();
                 self._draw();
             }, false);
         },*/

        /*画扇形*/
        _drawArc: function _drawArc() {
            this.ctx.save();
            var fillStyle = this.options.fillStyle;
            if (!fillStyle) return;

            for (var i = 0; i < this.size; i++) {
                var sAngle = this.sAngle + this.angle * i;
                this.ctx.beginPath();
                this.ctx.fillStyle = fillStyle[i % fillStyle.length];
                this.ctx.arc(this.radius, this.radius, this.innerRadius, sAngle, sAngle + this.angle, false);
                this.ctx.arc(this.radius, this.radius, this.outerRadius, sAngle + this.angle, sAngle, true);
                this.ctx.closePath();
                this.ctx.fill();
            }
            this.ctx.restore();
        },
        /*画扇形上的文字*/
        _drawText: function _drawText() {
            var fonts = this.options.font;
            for (var i = 0; i < this.size; i++) {
                var textArr = this._cleverBreak(this.options.products[i].name);
                this.ctx.save();
                this.ctx.translate(this.radius, this.radius);
                this.ctx.rotate(this.angle * i);
                this.ctx.fillStyle = fonts.color;
                this.ctx.font = this._getFontStyle();

                this.ctx.fillText(textArr[0], -this.ctx.measureText(textArr[0]).width / 2, -fonts.y);
                console.log(textArr[0]);
                if (textArr[1]) {
                    var y = -(fonts.y - parseInt(fonts.size * this.options.scale) * this.options.font.lineHeight);
                    this.ctx.fillText(textArr[1], -this.ctx.measureText(textArr[1]).width / 2, y);
                }
                this.ctx.restore();

                this.options.products[i].imgurl && this.imgurl.push(this.options.products[i].imgurl);
            }
        },
        /*文字断行*/
        _cleverBreak: function _cleverBreak(str) {
            var res,
                keys = this.options.breakText;
            if (!str) {
                return res;
            }
            for (var i = 0; i < keys.length; i++) {
                var idx = str.indexOf(keys[i]);
                if (idx > -1) {
                    res = [str.substr(0, idx), str.substr(idx)];
                    break;
                }
            }
            console.log(res + '|' + str);
            return res || [str];
        },
        /*绘制图片*/
        _drawImg: function _drawImg() {
            var self = this,
                width = this.options.images.width,
                height = this.options.images.height;

            this._loadImg(this.imgurl, function (img) {
                var scale = self.options.scale;
                for (var i = 0; i < img.length; i++) {
                    var ret = img[i],
                        w = (width || ret.width) * self.options.dpr * scale,
                        h = (height || ret.height) * self.options.dpr * scale;
                    self.ctx.save();
                    self.ctx.translate(self.radius, self.radius);
                    self.ctx.rotate(self.angle * i);
                    self.ctx.drawImage(ret, -w / 2, -self.options.images.y, w, h);
                    self.ctx.restore();
                };
            });
        },
        /*绘制画布*/
        _draw: function _draw() {
            var options = this.options,
                products = options.products,
                cx = this.cx,
                imgArr = [];

            this.ctx.clearRect(0, 0, this.diameter, this.diameter);
            this._drawArc();
            this._drawText();
            this._drawImg();
        },
        /*加载要绘制的图片*/
        _loadImg: function _loadImg(srcArr, callback) {
            var img = document.createElement('img');
            var cur = cur || 0;
            var self = this;

            if (!self.cur) {
                self.cur = 0;
                self.oImages = [];
            }

            var src = srcArr[self.cur++];
            img.src = src + '?' + self.cur;

            img.onload = function () {
                self.oImages.push(this);
                if (self.cur < srcArr.length) {
                    self._loadImg(srcArr, callback);
                } else {
                    self.cur = false;
                    callback(self.oImages);
                }
                this.onload = false;
            };

            img.onerror = function () {
                //console.log(this.src);
            };
        },
        /*开始旋转转盘*/
        _beginRotate: function _beginRotate() {
            var self = this,
                iSpeed = this.options.speed;
            self.isOver = false;
            clearInterval(self.timer);
            var cSpeed = 0,
                is = 0;

            self.timer = setInterval(function () {
                is = Math.ceil(iSpeed - cSpeed) / iSpeed;
                is = is > 0 ? Math.ceil(is) : Math.floor(is);
                cSpeed += is;
                if (cSpeed > iSpeed) {
                    is = iSpeed;
                }
                self.setRotateStyle(self.rotate + cSpeed);
                self.rotate += cSpeed;
            }, 40);
        },
        /*开始抽奖*/
        _start: function _start() {
            var self = this;
            if (!this.options.handler) return;
            this._fastClick(this.options.handler, function (ev) {
                if (self.hasClass(this, 'disabled')) return;
                if (self.isOver) {
                    self._beginRotate();
                    typeof self.options.handlerCallback === 'function' && self.options.handlerCallback.call(this, self);
                }
            });
        },
        _off: function _off(elemet, eventType, callback) {
            elemet.removeEventListener(eventType, callback, false);
        },
        _fastClick: function _fastClick(elemet, callback) {
            if ('ontouchstart' in document) {
                elemet.addEventListener('touchstart', callback, false);
            } else {
                elemet.addEventListener('click', callback, false);
            }
        },
        /*简单的继承*/
        extend: function extend(source, distance) {
            for (var attr in distance) {
                if (distance[attr] !== undefined) {
                    source[attr] = distance[attr];
                }
            }
            return source;
        },
        /*设置旋转*/
        setRotateStyle: function setRotateStyle(rotate) {
            this.prefix('transform', 'rotate(' + rotate + 'deg)');
        },
        prefix: function prefix(attr, val) {
            if (!val) return;
            var _fix = ['moz', 'o', 'webkit'],
                self = this;
            _fix.map(function (item) {
                self.oCanvas.style[item + self.capitalize(attr)] = val;
            });
            self.oCanvas.style[attr] = val;
        },
        capitalize: function capitalize(str) {
            return str.substr(0, 1).toUpperCase() + str.substr(1);
        },
        /*判断指定className是否存在*/
        hasClass: function hasClass(elemet, className) {
            var cls = elemet.className;
            return !!cls.match(new RegExp('\\b' + className + '\\b'));
        },
        /*停止转动, idx为指定停止的位置*/
        stop: function stop(idx, callback) {
            if (this.options.interval > 0) {
                this._stop(idx, function (_interface) {
                    _interface.isOver = false;
                    _interface.intervalTimer = setTimeout(function () {
                        _interface.isOver = true;
                        callback && callback(_interface);
                    }, _interface.options.interval);
                });
            } else {
                this._stop.apply(this, arguments);
            }
        },
        _stop: function _stop(idx, callback) {
            var stopTimer = null,
                self = this,
                iAngle = 360 / self.size;
            var iTarget = self.rotate + 360 * 5 + (self.size - idx - self.rotate % 360 / iAngle) * iAngle;
            var speedRatio = 35 - this.options.speed;

            this.timer && clearInterval(this.timer);
            function move() {
                var iSpeed = (iTarget - self.rotate) / speedRatio;
                iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);

                if (iSpeed > self.options.speed) {
                    iSpeed = self.options.speed;
                }
                self.rotate += iSpeed;
                self.rotate >= iTarget && (self.rotate = iTarget, self.isOver = true);
                self.setRotateStyle(self.rotate);

                if (self.isOver) {
                    clearInterval(stopTimer);callback && callback(self);
                }
            }
            stopTimer = setInterval(move, 40);
        },
        /*设置配置项*/
        _setOptions: function _setOptions(options) {
            /*默认配置项*/
            var config = {
                /*点击抽奖元素*/
                handler: '',
                /*点击抽奖的回调*/
                handlerCallback: function handlerCallback(_interface) {},
                outerRadius: 150,
                innerRadius: 0,
                /*循环填充数组颜色*/
                fillStyle: ['#ffdf8a', '#fffdc9'],
                /*请参考 flexible*/
                dpr: 1,
                /*重复触发的间距时间*/
                interval: 1000,
                /*速度5-30越大越快*/
                speed: 8,
                /*canvas css3运动样式*/
                ///transition: 'transform .3s linear',
                //transition: '',
                /*字体位置与样式*/
                /*画布显示缩放比例,值为1 安卓模糊*/
                scale: 4,
                font: {
                    y: '50%',
                    color: '#ee6500',
                    style: 'normal',
                    weight: 500,
                    size: '12px',
                    lineHeight: 1,
                    family: 'Arail'
                },
                /*图片位置与尺寸*/
                images: {
                    y: '88%',
                    width: 32,
                    height: 32
                },
                /*打断文字换行*/
                breakText: ['勋章', '勋章', '星豆'],
                /*礼物*/
                products: [
                    /*{
                        imgurl: 'http://',
                        text: '苹果手机',
                    }*/
                ]
            };
            return this.extend(config, options);
        }
    };

    window.Lottery = Lottery;
})();

},{}]},{},[1]);
